<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            left: 0;
            top: 0;
        }
        
        .box2 {
            width: 100px;
            height: 100px;
            background-color: blueviolet;
            position: absolute;
            left: 300px;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
<script>
    var box1Ele = document.querySelector(".box1");
    var box2Ele = document.querySelector(".box2");

    box1Ele.onmousedown = function(e) {
        var x = e.clientX - this.offsetLeft;
        var y = e.clientY - this.offsetTop;
        this.onmousemove = function(e) {
            var xx = e.clientX;
            var yy = e.clientY;
            this.style.left = xx - x + "px";
            this.style.top = yy - y + "px";

            if (
                Math.abs(box2Ele.offsetLeft - this.offsetLeft) < this.offsetWidth &&
                Math.abs(box2Ele.offsetTop - this.offsetTop) < this.offsetHeight
            ) {
                console.log("x轴上碰撞了");
            }
        };

        // getImageData

        document.onmouseup = function() {
            box1Ele.onmousemove = "";
        };
    };
</script>

</html>